#include <iostream>
#include <string>
#include <iostream>
#include <fstream>
using std::string;
string playfairalphabet[5]= {"abcde","fghik","lmnop","qrstu","vwxyz"};


class Playfair {
	/*
	If both letters are the same (or only one letter is left), add an "X" after the first letter. Encrypt the new pair and continue. Some variants of Playfair use "Q" instead of "X", but any uncommon monograph will do.
	If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row).
	If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column).
	If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first letter of the encrypted pair is the one that lies on the same row as the first letter of the plaintext pair.
	*/
public:
	//variables
	string key;
	string message;
	string encrypted;
	string version;
	
	Playfair(string message)
	{
		//constructor
		
	};
	
	~Playfair()
	{
		//desctructor
		std::cout << "Line encrypted" << std::endl;
	}
	
	//member functions
	void encrypt(){
		//cumulation of encryption related processes
		updatealphabet();
		removeAllWhite(message);
		handleuppercase(message);
		handledouble(message);
		handleodd(message);
		encrypted=getletterpairs(message);
	}
	
	void finalprep(){
		//adds the \n to a line of text, to write it to the text file
	}
	void updatealphabet(){
		
	}

	void removeAllWhite (std::string &str){
	    std::string temp;
	    for (unsigned int i = 0; i < str.length(); i++)
	        if (str[i] != ' ') temp += str[i];
	    str = temp;
	}

	void handledouble(string &message){
		int v;
		for (v=0; v != message.size()-1; ++v)
			if (message[v]==message[v+1]){
				message.insert(v+1, "x");
			}
	}

	void handleodd(string &message){
		if (message.size() % 2 != 0){
			message.append("x");
		}
	}
	
	void handleuppercase(string &message){
		for (int i=0;i<strlen(message.c_str());i++) 
        	if (message[i] >= 0x41 && message[i] <= 0x5A) 
            	message[i] = message[i] + 0x20;
	}
	
	string getletterpairs(string &message){
		string answers;
		int sets = (message.size()/2);
		//std::cout << sets << std::endl;
		int loop;
		for (loop=0; loop < sets; loop++)
			//std::cout << replaceletters(message[2*loop],message[2*loop+1]) << std::endl;
			answers+=replaceletters(message[2*loop],message[2*loop+1]);
		return answers;	
	}

	string replaceletters(char &first, char &second){
		//std::cout << "This part is running correctly." << std::endl;
		int loop;
		int loop2;
		int first1;
		int first2;
		int secondx;
		int secondy;
		string answer;

		for (loop=0; loop < 5; loop++)
			for (loop2=0; loop2<5; loop2++)
					if (first == playfairalphabet[loop][loop2]){
						first1= loop;
						first2= loop2;
					}
		for (loop=0; loop < 5; loop++)
			for (loop2=0; loop2<5; loop2++)
					if (second == playfairalphabet[loop][loop2]){
						secondx=loop;
						secondy=loop2;
					}

		if (first1==secondx){
			if (first2 != 4){
				answer+=playfairalphabet[first1][first2+1];
				if (secondy != 4){
					answer +=playfairalphabet[secondx][secondy+1];
				}
				else {
					answer +=playfairalphabet[secondx][0];			
				}
			}
			else {
				answer += playfairalphabet[first1][0];
				if (secondy != 4){
					answer +=playfairalphabet[secondx][secondy+1];
				}
				else {
					answer +=playfairalphabet[secondx][0];			
				}
			}
		}
		if (first2 == secondy){
			if (first1 != 4){
				answer+=playfairalphabet[first1+1][first2];
				if (secondx != 4){
					answer +=playfairalphabet[secondx+1][secondy];
				}
				else {
					answer +=playfairalphabet[0][secondy];			
				}
			}
			else {
				answer += playfairalphabet[0][first2];
				if (secondy != 4){
					answer +=playfairalphabet[secondx+1][secondy];
				}
				else {
					answer +=playfairalphabet[0][secondy];			
				}
			}
		}
		answer+=playfairalphabet[first1][secondy];
		answer+=playfairalphabet[secondx][first2];
		return answer;
	}
};